
from collections import deque


num,k=[int(i) for i in input().split()]

named={}
for i in range(num):
    sget = input().split()
    named[sget[0]]=int(sget[1])

num,end_row=divmod(num,k)
each_row=num
num+=end_row

cur=0
res=sorted(named.items(),key=lambda x:(-x[1],x[0])) 


## 70ms 6112KB 1/6个格式错误
def twolist(cur,num):
    for row in range(k): 
        mid=res[cur][0]
        cur+=1
        left,right=[],[]
        for col in range(num-1):
            if col%2==0:
                left.append(res[cur][0])
            else:
                right.append(res[cur][0])
            cur+=1
        
        num=each_row
        print(" ".join(left[::-1]),mid," ".join(right))

## 84ms 6784KB
def line_deque(cur,num):
    for row in range(k): 
        line=deque()
        line.append(res[cur][0])
        cur+=1
        for col in range(num-1):
            if col%2==0:
                line.appendleft(res[cur][0])
            else:
                line.append(res[cur][0])
            cur+=1
        
        num=each_row
        print(*line,sep=" ")


